@using Stl.OS
@using Stl.DependencyInjection
@implements IDisposable
@inject IServiceProvider Services;
@inject BlazorCircuitContext BlazorCircuitContext
@inject ISessionResolver SessionResolver

<CascadingAuthState UsePresenceReporter="true">
    <ThemeProvider Theme="@Theme">
        <Router AppAssembly="@typeof(Program).Assembly">
            <Found Context="routeData">
                <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
            </Found>
            <NotFound>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p>Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </ThemeProvider>
</CascadingAuthState>

@code {
    private Theme Theme { get; } = new() { IsGradient = true, IsRounded = false };

    [Parameter]
    public string SessionId { get; set; } = "";

    protected override void OnInitialized()
    {
        SessionResolver.Session = OSInfo.IsWebAssembly
            ? Session.Default
            : new Session(SessionId);
        if (!BlazorCircuitContext.IsPrerendering)
            BlazorCircuitContext.RootComponent = this;
        if (OSInfo.IsWebAssembly) {
            var rootServices = Services.Commander().Services;
            _ = rootServices.HostedServices().Start();
        }
    }

    public void Dispose()
        => BlazorCircuitContext.Dispose();
}
